package jw.hospital.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.util.BeanUtil;
import jw.hospital.hosp.model.hospital.Hospital;
import jw.hospital.hosp.vo.HospitalQueryVo;
import jw.hospital.respository.HospitalRespository;
import jw.hospital.service.HospitalService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import sun.security.krb5.internal.crypto.Des;

import java.util.Date;
import java.util.Map;

@Service
public class HospitalServiceImpl  implements HospitalService {
    @Autowired
    private HospitalRespository  hospitalRespository;

    @Override
    public void save(Map<String, Object> paramMap) {
        String jsonString = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(jsonString, Hospital.class);
        String hoscode = hospital.getHoscode();
        Hospital  hospitalExist = hospitalRespository.getHospitalByHoscode(hoscode);
       if(hospitalExist!=null){
           hospital.setStatus("0");
           hospital.setUpdateTime(new Date());
           hospital.setIsDeleted("0");
           hospitalRespository.save(hospital);
       }else {
           hospital.setStatus("0");
           hospital.setCreateTime(new Date());
           hospital.setUpdateTime(new Date());
           hospital.setIsDeleted("0");
           hospitalRespository.save(hospital);
       }

    }

    @Override
    public Hospital getHospitalByHosCode(String hoscode) {
      Hospital hospital =  hospitalRespository.getHospitalByHoscode(hoscode);
      return hospital ;
    }

    @Override
    public Page<Hospital> getHospitalPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Pageable pageable = PageRequest.of(page-1,limit, Sort.Direction.ASC);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Hospital hospital =new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        Example example = Example.of(hospital,matcher);
        Page pageList = hospitalRespository.findAll(example, pageable);

        return pageList;
    }
}
